package com.titar.robot.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DialerKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.titar.robot.R;


/**
 * Created by window on 2017/5/25.
 */

public class IconEditText extends LinearLayout {
    public static final String DEFAULT_NAMESPACE = "http://schemas.android.com/apk/res/android";

    private static final int DEFAULT_ID = - 1;

    private Context mContext;

    private ImageView mIvIcon;
    private DeleteEditText mEtText;

    public IconEditText(Context context) {
        this(context, null);
    }

    public IconEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.view_icon_edit_text, this, true);

        mIvIcon = (ImageView) findViewById(R.id.iv_icon);
        mEtText = (DeleteEditText) findViewById(R.id.ed_text);

        obtainAttributes(attrs);
    }

    private void obtainAttributes(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.IconEditText);

        int iconId = a.getResourceId(R.styleable.IconEditText_textIcon, DEFAULT_ID);
        int backcolor=a.getInteger(R.styleable.IconEditText_backcolor,DEFAULT_ID);
        int textcolor=a.getInteger(R.styleable.IconEditText_textcolor,DEFAULT_ID);
        int texthidColor=a.getInteger(R.styleable.IconEditText_texthitcolor,DEFAULT_ID);
        boolean phone = a.getBoolean(R.styleable.IconEditText_phone, false);
        boolean password = a.getBoolean(R.styleable.IconEditText_password, false);
        boolean editeble = a.getBoolean(R.styleable.IconEditText_editable, true);
        String text = a.getString(R.styleable.IconEditText_text);
        String hint = a.getString(R.styleable.IconEditText_hint);
        final String digits=a.getString(R.styleable.IconEditText_digits);
        a.recycle();

        if (backcolor!=DEFAULT_ID){
            setBackgroundColor(backcolor);
        }
        if (textcolor!=DEFAULT_ID)
            mEtText.setTextColor(textcolor);
        if (texthidColor!=DEFAULT_ID)
            mEtText.setHintTextColor(texthidColor);

        if(iconId != DEFAULT_ID)
            mIvIcon.setImageResource(iconId);

        if(phone) {
            mEtText.setInputType(InputType.TYPE_CLASS_PHONE);
        }else{
            mEtText.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        Log.i("password", "didits " + digits);

        if(password) {
            Log.i("password", "password 11111 " + password);
            mEtText.setIsPassword(password);
          // mEtText.setInputType(InputType.TYPE_CLASS_TEXT);
           // mEtText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }else{
            Log.i("password", "password false " + password);
            mEtText.setIsPassword(false);
           //mEtText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }

       /* if(password)
            // 显示为密码
            mEtText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);*/

        setEditbled(editeble);

        mEtText.setText(text);
        mEtText.setHint(hint);
if(!TextUtils.isEmpty(digits)){
    mEtText.setmyDigits(digits);

}
    }

    public void setDigits(final String digt){
        mEtText.setKeyListener(new DialerKeyListener(){
            @Override
            protected char[] getAcceptedChars() {
                return digt.toCharArray();
            }
        });
        mEtText.setKeyListener(DigitsKeyListener.getInstance(digt));
    }

    public void setIsPassword(boolean isPassword){
        if(isPassword) {
             mEtText.setInputType(InputType.TYPE_CLASS_TEXT);
             mEtText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }else{
            mEtText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }


    public void setImageResource(int resId) {
        mIvIcon.setImageResource(resId);
    }

    public void setText(CharSequence text) {
        mEtText.setText(text);
    }

    public Editable getText() {
        return mEtText.getText();
    }

    public void setHint(CharSequence text) {
        mEtText.setHint(text);
    }

    public void setEnabled(boolean enabled) {
        mEtText.setEnabled(enabled);
    }

    public void setMaxLength(int length) {
        mEtText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(length)});
    }

    public  void setEllipsize(){
        mEtText.setEllipsize(TextUtils.TruncateAt.END);
        mEtText.setSingleLine();
    }

    public void setEditbled(boolean editeble) {
        mEtText.setEnabled(editeble);
        mEtText.setFocusable(editeble);
    }

    public void setSelection(int length){
        mEtText.setSelection(length);
    }

    private String getDefaultNamespaceAttributeValue(AttributeSet attrs, String attribute) {
        String value = null;

        int resId = attrs.getAttributeResourceValue(DEFAULT_NAMESPACE, attribute, 0);
        if(resId != 0) {
            value = getContext().getResources().getString(resId);
        } else {
            value = attrs.getAttributeValue(DEFAULT_NAMESPACE, attribute);
        }

        return value;
    }
}
